package cn.iocoder.yudao.module.nmkj.controller.admin.culture;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.nmkj.controller.admin.culture.vo.*;
import cn.iocoder.yudao.module.nmkj.dal.dataobject.culture.CultureDO;
import cn.iocoder.yudao.module.nmkj.service.culture.CultureService;

@Tag(name = "管理后台 - 企业文化")
@RestController
@RequestMapping("/nmkj/culture")
@Validated
public class CultureController {

    @Resource
    private CultureService cultureService;

    @PostMapping("/create")
    @Operation(summary = "创建企业文化")
    @PreAuthorize("@ss.hasPermission('nmkj:culture:create')")
    public CommonResult<Long> createCulture(@Valid @RequestBody CultureSaveReqVO createReqVO) {
        return success(cultureService.createCulture(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新企业文化")
    @PreAuthorize("@ss.hasPermission('nmkj:culture:update')")
    public CommonResult<Boolean> updateCulture(@Valid @RequestBody CultureSaveReqVO updateReqVO) {
        cultureService.updateCulture(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除企业文化")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('nmkj:culture:delete')")
    public CommonResult<Boolean> deleteCulture(@RequestParam("id") Long id) {
        cultureService.deleteCulture(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得企业文化")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('nmkj:culture:query')")
    public CommonResult<CultureRespVO> getCulture(@RequestParam("id") Long id) {
        CultureDO culture = cultureService.getCulture(id);
        return success(BeanUtils.toBean(culture, CultureRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得企业文化分页")
    @PreAuthorize("@ss.hasPermission('nmkj:culture:query')")
    public CommonResult<PageResult<CultureRespVO>> getCulturePage(@Valid CulturePageReqVO pageReqVO) {
        PageResult<CultureDO> pageResult = cultureService.getCulturePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CultureRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出企业文化 Excel")
    @PreAuthorize("@ss.hasPermission('nmkj:culture:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCultureExcel(@Valid CulturePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CultureDO> list = cultureService.getCulturePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "企业文化.xls", "数据", CultureRespVO.class,
                        BeanUtils.toBean(list, CultureRespVO.class));
    }

}